package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import com.qk.dto.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserMapper userMapper;


    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        List<User> userList = userMapper.selectList(userDto);
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(),page.getResult());

    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        String password= user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        userMapper.insert(user);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    @Override
    public User getByid(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void update(User user) {
        String password= user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public List<User> listByRole(String roleLabel) {
        return userMapper.listByRole(roleLabel);
    }

    @Override
    public LoginResultVo login(User user) {
       User u = userMapper.selectByUsername(user.getUsername());
       if(u == null){
           log.info("用户不存在");
           throw new BusinessException("用户名或密码错误");
       }
       //判断用户密码是否正确
        String pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        System.out.println("密码是------" + pass);
        if(!u.getPassword().equals(pass)){
            log.info("密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        //判断状态
        if(u.getStatus() != 1){
            log.info("用户停用");
            throw new BusinessException("用户停用");
        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());




        //调用方法生成令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}
